package com.nd.te.prepare;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

public class preprocess {
    static int count=0;
    public static class datamapper1 extends Mapper<LongWritable, Text, IntWritable, global>{
        @Override
        protected void map(LongWritable key, Text line, Mapper<LongWritable, Text, IntWritable,global>.Context context) throws IOException, InterruptedException {
            String line1 = new String(line.getBytes(),0,line.getLength(),"UTF-8");
            String[] cc = line1.split(",");
            global gt =new global();
            if (cc.length == 9) {//洗去含空值的数据
                int temp = Integer.parseInt(cc[0]);
                if(temp != 2020){//将数据每十年分类
                    temp = temp - temp % 10 + 10;
                }
                if(cc[8]!="1"&&cc[8]!="2"&cc[8]!="3"&&cc[8]!="4"&&cc[8]!="5"&&cc[8]!="6"&&cc[8]!="7"&cc[8]!="8"&&cc[8]!="9") {
                    cc[8]="10";
                }//去除最后一列不符合预料的数据
                gt.set(temp,Integer.parseInt(cc[1]),cc[2],cc[3],cc[4],cc[5],cc[6],cc[7],Integer.parseInt(cc[8]));
                IntWritable i = new IntWritable();
                i.set(count);
                context.write(i, gt);
                count++;
            }
        }
    }

    public static class datareducer1 extends Reducer<IntWritable,global,global,NullWritable>{
        protected void reduce(IntWritable key,Iterable<global> values, Reducer<IntWritable,global,global,NullWritable>.Context context) throws IOException, InterruptedException{
            global k = values.iterator().next();
            context.write(k, NullWritable.get());
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        //args=new String[]{"hdfs://hadoop101:8020/wc/input/data.txt","hdfs://hadoop101:8020/wc/datapro1.txt"};
        //args = new String[]{"C:\\Users\\张传畅\\Desktop\\bigwork\\data.txt", "C:\\Users\\张传畅\\Desktop\\bigwork\\out"};

        args = new String[]{"data/input/data.txt","data/preout"};

        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        job.setMapperClass(datamapper1.class);
        job.setReducerClass(datareducer1.class);

        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(global.class);

        job.setOutputKeyClass(global.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
